/*
 * @Author: yangni 920556271@qq.com
 * @Date: 2024-01-16 09:32:27
 * @LastEditors: yangni 920556271@qq.com
 * @LastEditTime: 2024-01-19 10:24:23
 * @FilePath: /yt-devops/lib/pages/devices/device_controll/device_property_group_render.dart
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:yt_devops/components/gaps.dart';
import 'package:yt_devops/model/device_obj/properties_model.dart';
import 'package:yt_devops/model/device_obj/property_group_model.dart';
import 'package:yt_devops/pages/devices/device_controll/device_controll_utils.dart';
import 'package:yt_devops/styles/styles.dart';
import 'package:yt_devops/utils/enums.dart';
import 'package:yt_devops/utils/func_utils.dart';

class DevicePropertyGroupRender extends StatefulWidget {
  final PropertyGroupModel propertyGroupModel;
  final ConnectStatusType? connectStatus;
  final Map<String, dynamic> dataSource;
  const DevicePropertyGroupRender({
    super.key,
    required this.propertyGroupModel,
    required this.dataSource,
    required this.connectStatus,
  });

  @override
  State<DevicePropertyGroupRender> createState() =>
      _DevicePropertyGroupRenderState();
}

class _DevicePropertyGroupRenderState extends State<DevicePropertyGroupRender> {
  @override
  Widget build(BuildContext context) {
    List<PropertiesModel> children = widget.propertyGroupModel.children ?? [];
    return Column(
      children: [
        Gaps.line,
        Offstage(
          offstage: widget.propertyGroupModel.showType ==
              DeviceModelShowType.hideName,
          child: DeviceControllUtils.createHead(
            widget.propertyGroupModel.name ?? '',
            isShowSetButton: false,
          ),
        ),
        ListView.separated(
          physics: const NeverScrollableScrollPhysics(),
          shrinkWrap: true,
          itemBuilder: (context, index) => Container(
            color: Colour.white,
            padding: EdgeInsets.only(left: 16.w),
            child: textWidget(children[index]),
          ),
          separatorBuilder: (context, index) => Gaps.line,
          itemCount: children.length,
        )
      ],
    );
  }

  Color get contentTextColor => widget.connectStatus == ConnectStatusType.on
      ? Colour.mainTextColor
      : Colour.lightTextColor;

  Widget textWidget(
    PropertiesModel model,
  ) {
    return Container(
      color: Colour.white,
      height: 50,
      child: Row(
        children: [
          Text(
            model.name ?? '',
            style: TextStyle(fontSize: 14.sp, color: const Color(0xFF666666)),
          ),
          Text(
            ': ',
            style: TextStyles.lf14,
          ),
          Text(
            FuncUtils.formatModelValue(
                    widget.dataSource[DeviceControllUtils.getPropertyId(model)],
                    model.dataType)
                .toString(),
            style: TextStyle(
              fontSize: 14.sp,
              color: contentTextColor,
              fontWeight: FontWeight.w500,
            ),
          ),
        ],
      ),
    );
  }
}
